import { Cmd2dDrawText, Cmd2dFillRect, Cmd2dPushFont, Cmd2dSetFillColor, KaseoCommon2dGDI } from "../../dev/KaseoCanvasGraphicsDI";
import { KASEO_DEV_KEYBOARD_GI } from "../../dev/KaseoKeyboardDI";
import { KaseoRuntimeBridge } from "../../KaseoRuntime";
import { RenderLayoutContext } from "../IRenderable";
import WidgetBase, { WidgetStyle } from "./WidgetBase";


export interface LabelWidgetStyle {
    fontSize?: number
}


export default class WidgetLabel extends WidgetBase {
    label: string
    actived: boolean = false

    constructor(label: string, style: LabelWidgetStyle & WidgetStyle) {
        super(style)
        
        this.label = label
    }

    render(gdi: KaseoCommon2dGDI, ctx: RenderLayoutContext): void {
        let width = this.style.width

        const {fontSize = 12} = <LabelWidgetStyle>this.style

        if (width == 'auto') {
            width = ctx.preferWidth
        }

        if (this.state.focused) {
            gdi.pushCommand2d(new Cmd2dSetFillColor(0, 0, 0))
            gdi.pushCommand2d(new Cmd2dFillRect(0, 0, width, this.style.height))
            gdi.pushCommand2d(new Cmd2dSetFillColor(1, 1, 1))
        } else {
            gdi.pushCommand2d(new Cmd2dSetFillColor(1, 1, 1))
            gdi.pushCommand2d(new Cmd2dFillRect(0, 0, width, this.style.height))
            gdi.pushCommand2d(new Cmd2dSetFillColor(0, 0, 0))
        }

        gdi.setContextFontSize(fontSize)

        gdi.pushCommand2d(new Cmd2dPushFont())
        if (this.actived) {gdi.pushCommand2d(
            new Cmd2dSetFillColor(1, 0, 0))
        }
        gdi.pushCommand2d(new Cmd2dDrawText(this.label, 3, this.style.height - 3))
    }

    onKonck(bridge: KaseoRuntimeBridge): void {
        if (bridge.getKnockDevType() == KASEO_DEV_KEYBOARD_GI) {
            const kdi = bridge.getMajorKeyboardDI()

            if (kdi?.getKeyDown(132)) {
                this.actived = !this.actived
                this.requestRender()
            }
        }
    }
}
